<template>
  <div class="app-container">
    <el-button type="success" icon="el-icon-finished" style="margin: 10px;" @click="handleSubmit">
      提交
    </el-button>
    <el-button type="info" icon="el-icon-refresh" style="margin: 10px;" @click="handleRefresh">
      重置
    </el-button>
    <el-button type="info" icon="el-icon-circle-close" style="margin: 10px;" @click="handleClose">
      关闭
    </el-button>
    <el-button type="danger" icon="el-icon-delete" style="margin: 10px;" @click="handleDelete">
      删除
    </el-button>

    <el-form ref="brandEditForm" :rules="brandRules" :model="brand" label-width="80px" style="margin-left:0px;">
      <el-form-item label="品牌名称" prop="name">
        <el-input v-model="brand.name" style="width: 400px;" />
      </el-form-item>
      <el-form-item label="品牌简称" prop="shortName">
        <el-input v-model="brand.shortName" style="width: 400px;" />
      </el-form-item>
      <el-form-item label="品牌排序" prop="brandOrder">
        <el-input v-model="brand.brandOrder" type="number" step="1" :min="1" :max="9999" style="width: 100px;" />
      </el-form-item>
      <el-form-item label="品牌介绍" prop="description">
        <el-input v-model="brand.description" type="textarea" :rows="4" style="width: 400px;" />
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { getBrandById, putBrandById, deleteBrandById } from '@/api/manager/business-api'

export default {
  name: 'ManagerBrandEdit',
  filters: {
  },
  data() {
    return {
      brand: {
        id: null,
        name: null,
        shortName: null,
        brandOrder: null,
        description: null
      },
      brandRules: {
        name: [{
          required: true, message: '请输入品牌名称', trigger: 'blur'
        }],
        shortName: [{
          required: true, message: '请输入品牌简称', trigger: 'blur'
        }],
        brandOrder: [{
          required: true, message: '请输入品牌排序', trigger: 'blur'
        }]
      }
    }
  },
  async created() {
    await this.initLoad()
    await this.loadDetail()
  },
  methods: {
    async initLoad() {
    },
    loadDetail() {
      getBrandById(this.$route.params.id).then(response => {
        const { data } = response
        this.brand = data
      })
    },

    handleSubmit() {
      this.$refs.brandEditForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.brand))
        putBrandById(this.brand.id, data).then(response => {
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 1 * 1000
          })
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    },
    handleRefresh() {
      this.loadDetail()
    },
    handleClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    },
    handleDelete() {
      deleteBrandById(this.brand.id).then(response => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 1 * 1000
        })
        this.handleClose()
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
      })
    }

  }
}

</script>
